package cn.elwy.rcp.ui.explorer.core.service.impl;

import java.net.URL;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.elwy.osgi.config.util.ResourceUtil;
import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.explorer.core.PathConst;
import cn.elwy.rcp.ui.explorer.core.dao.BuilderModuleConfigDao;
import cn.elwy.rcp.ui.explorer.core.handler.BuilderModuleChoiceHandler;
import cn.elwy.rcp.ui.explorer.core.service.BuilderModuleChoiceService;
import cn.elwy.rcp.ui.explorer.core.util.SVNClient;
import cn.elwy.rcp.ui.explorer.core.util.SvnUtil;
import cn.elwy.rcp.ui.explorer.core.vo.BuilderModule;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;

@Service(BuilderModuleChoiceService.ID)
public class BuilderModuleChoiceServiceImpl implements BuilderModuleChoiceService {

	@Resource
	private BuilderModuleConfigDao builderModuleConfigDao;

	@SuppressWarnings("unchecked")
	@Override
	public List<BuilderModule> getBuilderModules(ProjectConfig projectConfig, ProgressMonitor monitor) {
		// Map<String, String> outMap = builderModuleConfigDao.load(new File(""));

		SVNClient clientManager = null;
		try {
			clientManager = SvnUtil.getClientManager(projectConfig.getScmUrl(), projectConfig.getScmUser(),
					projectConfig.getScmPassword());

			URL url = ResourceUtil.getResource(this.getClass(), PathConst.MODULE_TEMPLATE_FILE);
			Map<String, String> outMap = builderModuleConfigDao.load(url);

			BuilderModuleChoiceHandler packer = new BuilderModuleChoiceHandler();
			packer.setSvnClient(clientManager);
			packer.setOutMap(outMap);

			return (List<BuilderModule>) packer.execute(projectConfig, monitor);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (clientManager != null) {
				clientManager.dispose();
			}
		}

		return null;
	}

	public BuilderModuleConfigDao getBuilderModuleConfigDao() {
		return builderModuleConfigDao;
	}

	public void setBuilderModuleConfigDao(BuilderModuleConfigDao builderModuleConfigDao) {
		this.builderModuleConfigDao = builderModuleConfigDao;
	}

}
